const colormap_select = 1
const colormap_sqrt = 0
const spectrum_scale_color_map_transparency = 200
const color_map = new Uint32Array(256)
const color_map_transparent = new Uint32Array(256)
const color_map_r = new Uint8Array(256)
const color_map_g = new Uint8Array(256)
const color_map_b = new Uint8Array(256)
let black_level, white_level
function mkcolormap() {
  for (let i = 0; i < 256; i++) {
    var r, g, b
    switch (colormap_select) {
      case 1:
        // new default
        if (i < 32) {
          r = 0
          g = 0
          b = (i * 255) / 31
        } else if (i < 72) {
          r = 0
          g = ((i - 32) * 255) / 39
          b = 255
        } else if (i < 96) {
          r = 0
          g = 255
          b = 255 - ((i - 72) * 255) / 23
        } else if (i < 116) {
          r = ((i - 96) * 255) / 19
          g = 255
          b = 0
        } else if (i < 184) {
          r = 255
          g = 255 - ((i - 116) * 255) / 67
          b = 0
        } else {
          r = 255
          g = 0
          b = ((i - 184) * 128) / 70
        }
        break
      case 2:
        // greyscale
        black_level = 0.0
        white_level = 48.0
        var v = black_level + Math.round(((255 - black_level + white_level) * i) / 255)
        if (v > 255) {
          v = 255
        }
        r = g = b = v
        break
      case 3:
        // user 1
        var c_from = { r: 255, g: 0, b: 255 }
        var c_to = { r: 0, g: 255, b: 0 }
        var s = 1.0
        r = c_from.r * (i / 255) + c_to.r * ((255 - i) / 255)
        r = Math.round(r * (s + (i / 255) * (1 - s)))
        g = c_from.g * (i / 255) + c_to.g * ((255 - i) / 255)
        g = Math.round(g * (s + (i / 255) * (1 - s)))
        b = c_from.b * (i / 255) + c_to.b * ((255 - i) / 255)
        b = Math.round(b * (s + (i / 255) * (1 - s)))
        break
      case 0:
      default:
        // old one from CuteSDR
        if (i < 43) {
          r = 0
          g = 0
          b = (i * 255) / 43
        } else if (i < 87) {
          r = 0
          g = ((i - 43) * 255) / 43
          b = 255
        } else if (i < 120) {
          r = 0
          g = 255
          b = 255 - ((i - 87) * 255) / 32
        } else if (i < 154) {
          r = ((i - 120) * 255) / 33
          g = 255
          b = 0
        } else if (i < 217) {
          r = 255
          g = 255 - ((i - 154) * 255) / 62
          b = 0
        } else {
          r = 255
          g = 0
          b = ((i - 217) * 128) / 38
        }
        break
    }
    color_map[i] = (r << 24) | (g << 16) | (b << 8) | 0xff
    color_map_transparent[i] =
      (r << 24) | (g << 16) | (b << 8) | spectrum_scale_color_map_transparency
    color_map_r[i] = r
    color_map_g[i] = g
    color_map_b[i] = b
  }
}
mkcolormap()
export { mkcolormap, color_map_r, color_map_g, color_map_b }
